package com.java.generic;

/**
 * 泛型类
 */
public class LearnGenericClass {
    // 一元泛型
    static class OneGenericClass<T> { // 泛型标识符T表示Type，此处可为任意标识符
        private T var;
        
        public T getVar() {return var;}
        
        public void setVar(T var) {this.var = var;}
    }
    
    // 多元泛型：多个泛型间使用,分隔
    static class ManyGenericClass<K, V> { // 泛型标识符K表示Key、V表示Value，此处可为任意标识符
        private K key;
        private V val;
        
        public K getKey() {return key;}
        
        public void setKey(K key) {this.key = key;}
        
        public V getVal() {return val;}
        
        public void setVal(V val) {this.val = val;}
    }
    
    public static void main(String[] args) {
        OneGenericClass<String> oneGenericClass = new OneGenericClass<>();
        oneGenericClass.setVar("SYX");
        // oneGenericClass.setVar(20); // 需要的类型String
        
        ManyGenericClass<Integer, String> manyGenericClass = new ManyGenericClass<>();
        manyGenericClass.setKey(20);
        manyGenericClass.setVal("SYX");
    }
}
